﻿#region Opis pliku
//-----------------------------------------------------------------------------
// Button.cs
// Autor: Jan Czarnowski
// 20.11.2012
//-----------------------------------------------------------------------------
#endregion

#region Using...

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

#endregion

namespace pogra
{
    /// <summary>
    /// Klasa przycisku. Używana w GamePlayGUI
    /// </summary>
    class Button
    {
        #region pola i wlasciwosci

        bool isPressed = false;
        bool isHidden = false;
        bool isActive = true;
        
        Texture2D _texture;
        Color _col = Color.White;
        Vector2 _pos;
        int _id;

        public Vector2 Pos
        {
            set { _pos = value; }
            get { return _pos; }
        }

        public bool IsPressed
        {
            get 
            {
                if (isPressed)
                {
                    isPressed = false;
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public bool IsActive
        {
            get { return isActive; }
            set { isActive = value; }
        }

        public bool IsHidden
        {
            get { return isHidden; }
            set { isHidden = value; }
        }

        public int ID
        {
            get { return _id; }
        }

        #endregion

        #region inicjalizacja

        public Button(Vector2 pos, Texture2D tex, int ID)
        {
            _pos = pos;
            _texture = tex;
            _id = ID;
        }

        #endregion

        #region metody draw i update

        public void draw(ExtendedSpriteBatch exSpriteBatch)
        {
            if(!isHidden)
                exSpriteBatch.Draw(_texture, new Rectangle((int)_pos.X,(int)_pos.Y, 44,46),_col);
        }

        public void update(TimeSpan gameTime)
        {
            if (!isActive)
                _col = Color.Gray * 0.4f;
        }

        #endregion

        #region metody publiczne

        public void HandleInput(InputState input)
        {
            Rectangle rect = new Rectangle((int)_pos.X, (int)_pos.Y, 44 ,46);//_texture.Width, _texture.Height);
            Point mousePos = new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y);

            if (rect.Contains(mousePos) && isActive)
            {
                _col = Color.Crimson * 0.8f;

                if (input.IsNewMLeftButtonPressed())
                    isPressed = true;
            }
            else if (isActive)
            {
                _col = Color.White;
                isPressed = false;
            }
            else
                isPressed = false;
        }

        #endregion
    }
}
